package test;

import static org.junit.Assert.*;
import main.*;
import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.Garage;
import unit.PoliceCar;
import unit.Unit;
import dispatch.DispatchCenter;
import emergency.*;

public class useCaseTestHandleEmergency {
	FacadeDispatcher facade;
	Location justALocation;
	
	DispatchCenter center;
	Garage garage;
	
	Emergency em1, em2, em3, em4;
	Unit u1;
	
	@Before
	public void setUp() throws Exception {
		String args[] = new String[1];
		args[0] = "environment.dat";
		World world = new World(args);
		facade = new FacadeDispatcher(world);
		
		center = facade.getDispatchCenter();
		garage = center.getGarage();
		justALocation = new Location(10,10,"NameDoesn'tMatter");
		
		em1 = new Fire(justALocation, Severity.NORMAL, center, Fire.Size.HOUSE, true, 1, 1);
		em2 = new PublicDisturbance(justALocation, Severity.NORMAL, center, 1);
		em3 = new TrafficAccident(justALocation, Severity.NORMAL, center, 1, 1);
		em4 = new Robbery(justALocation, Severity.NORMAL, center, false, true);
		
		u1 = new PoliceCar("Police", 60, garage);
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testEndEmergencyNoEmergency() throws Exception{
		//U1 doesn't have an emergency assigned
		u1.endEmergency();
	}
	
	
	@Test (expected=IllegalArgumentException.class)
	public void testEndEmergencyUnitNotAtEmergency() throws Exception{
		facade.assignAllUnits(em3);

		//unit we are going to test for correct handling
		Unit u = em3.getUnits().get(1);
		
		//after 1 minute, unit isn't at the emergency
		center.getClock().advanceTime(1);
		
		//simulate unit commander's behaviour ( pick unit that completed his task ) 
		u.endEmergency();
	}
	
	@Test
	public void testHandleEmergency() throws Exception{
		facade.assignAllUnits(em3);
		
		//unit we are going to test for correct handling
		Unit u = em3.getUnits().get(0);

		Garage uHomeLoc = u.getHomeLocation();
		
		center.getClock().advanceTime(100);
		
		//after 60 minutes, u is at the location of em3 
		assertTrue(u.getCurrentLocation().equals(em3.getLocation()));

		//simulate unit commander's behaviour ( pick unit that completed his task ) 
		u.endEmergency();
		
		center.getClock().advanceTime(100);

		assertTrue(uHomeLoc.getLocation().equals(u.getCurrentLocation()));
	}
	
	
	
	
	
	
	
	
	
}
